package com.scalar.chiptrack.operations.tapereel.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.tapereel.forms.TapeReelRequestForm;
import com.scalar.chiptrack.operations.tapereel.dao.TapeReelRequestDAO;
import com.scalar.chiptrack.operations.tapereel.TapeReelRequest;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.ArrayList;


/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Apr 16, 2007
 * Time: 6:23:15 PM
 */
public class TapeReelRequestSearchCommand extends ActionCommand
{

    private static final String ACTION_NAME = "/tapeReelRequest.do" ;

    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        TapeReelRequestForm tapeReelRequestForm = (TapeReelRequestForm) form;
        TapeReelRequest tapeReelRequest         = tapeReelRequestForm.getTapeReelRequest();
        ActionForward actionForward             = mapping.findForward("search");
        HashMap errors                          = new HashMap();
        HashMap info                            = new HashMap();

        try
        {

           ArrayList searchResultList = TapeReelRequestDAO.getAllTapeReelRequestDetails(tapeReelRequest );

           if( searchResultList != null && searchResultList.size() > 0 )
            {
                request.setAttribute( "searchList", searchResultList );
            }
            else
            {
                info.put( "Tape & Reel Request matching the given search criteria could not be found", null );
            }

            request.setAttribute("partNumbersList", TapeReelRequestDAO.loadTapeAndReelFgProducts() );
            request.setAttribute("vendorsList", TapeReelRequestDAO.getTrLocations());
            request.setAttribute("statusList", TapeReelRequest.getStatusList());
            tapeReelRequest.setReqStatus("Issued_Pulled");
            tapeReelRequestForm.setTapeReelRequest( tapeReelRequest );

            actionForward = mapping.findForward("search");
            request.setAttribute( INFO_MESSAGES, info );
            request.setAttribute( ERROR_MESSAGES, errors );
        }
        catch( Exception  e )
        {
            throw new ScalarException("Exception while getting all the Tape & Reel request details" + e);
        }

        return actionForward;

    }
}
